﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import reprise.commands.*;
    import reprise.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.controllers.data.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.computerdisplay.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class ComputerMediator extends Mediator implements IMediator {

		[Inject]
        public var computerDisplay:ComputerDisplay;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var tutorialModel:TutorialModel;
		[Inject]
        public var fileInfoModel:FileInfoModel;
		[Inject]
        public var environmentModel:EnvironmentModel;
		[Inject]
        public var world:WorldModel;
        private var _isNextPatientAllowed:Boolean = true;
        private var _patientQueue:Array;

        public function ComputerMediator(){
            this._patientQueue = [];
            super();
        }
        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_COMPUTER_DISPLAY, this.showComputerDisplay);
            eventMap.mapListener(eventDispatcher, EventConstants.HIDE_COMPUTER_DISPLAY, this.hideComputerDisplay);
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_PATIENT_VIEW, this.onCreatePatientView);
            eventMap.mapListener(eventDispatcher, GameEvent.LEVEL_UP, this.levelUp);
            addViewListener(DisplayEvent.HIDE_COMPLETE, this.computerDisplay_hideComplete);
            addViewListener(EventConstants.DO_TRACKING, this.doTracking);
            this.computerDisplay.setHeadlineLabel(this.languageModel.getTextByKey("reception_computer_headline"));
            if (this.userModel.level == 1){
                this.computerDisplay.addCSSClass("level1");
            };
        }
        private function levelUp(_arg1:InfoEvent):void{
            if (this.userModel.level == 2){
                this.computerDisplay.removeCSSClass("level1");
            };
        }
        private function hideComputerDisplay(_arg1:Event):void{
            if (this.computerDisplay.isOpen()){
                this.computerDisplay.hide();
            };
        }
        private function doTracking(_arg1:InfoEvent):void{
            this.track(_arg1.id);
        }
        private function computerDisplay_hideComplete(_arg1:Event):void{
            this.tickModel.pause = false;
            dispatch(new Event(EventConstants.ON_COMPUTER_DISPLAY_HIDDEN));
        }
        protected function showComputerDisplay(_arg1:InfoEvent):void{
            var _local2:Array;
            var _local3:Boolean;
            dispatch(new InfoEvent(EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER, null));
            l("SHOW DISPLAY");
            if (!this.computerDisplay.isOpen()){
                l("OPEN COMPUTER");
                if ((((this.tutorialModel.state > TutorialModel.FOUNTAIN_START_TUTORIAL)) && ((this.tutorialModel.state < TutorialModel.FOUNTAIN_FINISH_TUTORIAL)))){
                    return;
                };
                _local2 = _arg1.info.pets;
                _local3 = _arg1.info.areMorePetsAllowed;
                this.computerDisplay.morePetsAreAllowed = _local3;
                this.tickModel.pause = true;
                this.createPetBoxes(_local2);
                if (!_local3){
                    this.computerDisplay.createTreatSomePetsFirstLayer(this.languageModel.getTextByKey("reception_layer_nopetfree"), this.languageModel.getTextByKey("global_button_ok"));
                };
                this.computerDisplay.show();
                this.track("open");
            };
        }
        protected function createPetBoxes(_arg1:Array):void{
            var _local6:PatientPoolVO;
            var _local8:PetBox;
            var _local10:String;
            var _local11:Boolean;
            var _local2:String = this.languageModel.getTextByKey("global_needs");
            var _local3:String = this.languageModel.getTextByKey("reception_selectionbox_needrooms");
            var _local4:String = this.languageModel.getTextByKey("global_reward");
            var _local5:String = this.languageModel.getTextByKey("global_build_now");
            var _local7:PetBoxDisplayData = new PetBoxDisplayData();
            var _local9:int;
            while (_local9 < _arg1.length) {
                _local6 = _arg1[_local9];
                _local7.index = _local9;
                _local7.needsLabel = _local2;
                _local7.needsRoomsLabel = _local3;
                _local7.rewardsLabel = _local4;
                _local7.diseaseName = _local6.diseaseName;
                _local7.coins = _local6.coins;
                _local7.xp = _local6.xp;
                _local7.petType = _local6.petType;
                _local10 = ("pet_" + _local6.petType.toLowerCase());
                _local7.name = this.languageModel.getTextByKey(_local10);
                _local7.duration = _local6.duration;
                _local7.durationLabel = TimeUtils.getComputerDisplayDurationFormat(_local6.duration, this.languageModel.getTextByKey("reception_selectionbox_roomminutes"), this.languageModel.getTextByKey("reception_selectionbox_roomhours"), this.languageModel.getTextByKey("reception_selectionbox_roomhours_sing"), this.languageModel.getTextByKey("reception_selectionbox_roomdays"), this.languageModel.getTextByKey("reception_selectionbox_roomday"));
                _local7.missingRoomTypes = _local6.missingRoomTypes;
                _local7.treatments = _local6.treatments;
                _local7.hasMinLevel = _local6.hasMinLevel;
                _local7.minLevelLabel = this.languageModel.getTextByKey("global_levelnumber", {levelnumber:_local6.minLevel});
                _local7.diseaseId = _local6.diseaseId;
                _local7.buildNowButtonLabel = _local5;
                _local7.durationType = _local6.durationType;
                _local7.rewardIcon = _local6.rewardIcon;
                _local7.numberOfRewards = String(_local6.numberOfRewards);
                _local11 = this.world.hasAreaType(GameConstants.TYPE_FOUNTAIN);
                _local8 = this.computerDisplay.createPetBox(_local7, _local11);
                ((_local6.isLocked) || (_local8.addEventListener(MouseEvent.CLICK, this.petBox_click)));
                if (((_local6.isLocked) && (_local8.buildButton))){
                    _local8.buildButton.addEventListener(MouseEvent.CLICK, this.buildRoomButton_click);
                };
                _local9++;
            };
        }
        protected function buildRoomButton_click(_arg1:Event):void{
            this.computerDisplay.hide();
            var _local2:TimerCommand = new TimerCommand(200);
            _local2.addEventListener(Event.COMPLETE, this.delay_complete);
            _local2.execute();
            this.track(GameConstants.TRACK_BUILD_ROOM_BUTTON);
        }
        private function delay_complete(_arg1:Event):void{
            var _local2:TimerCommand = TimerCommand(_arg1.target);
            _local2.removeEventListener(Event.COMPLETE, this.delay_complete);
            dispatch(new Event(EventConstants.OPEN_SHOP));
            dispatch(new InfoEvent(EventConstants.SELECT_SHOP_CATEGORY, null, {category:ShopItemVO.CATEGORY_ROOMS}));
        }
        protected function petBox_click(_arg1:MouseEvent):void{
            if (!this.computerDisplay.isOpen()){
                return;
            };
            this.computerDisplay.hide();
            var _local2:PetBox = PetBox(_arg1.currentTarget);
            _local2.removeEventListener(MouseEvent.CLICK, this.petBox_click);
            this.track(_local2.petType);
            var _local3:Object = {pet_type:_local2.petType, disease_name:_local2.diseaseName, disease_id:_local2.diseaseId};
            if (this._isNextPatientAllowed){
                this.createPatientView(_local3);
            } else {
                this._patientQueue.push(_local3);
            };
        }
        private function onCreatePatientView(_arg1:InfoEvent):void{
            this._isNextPatientAllowed = true;
            if (this._patientQueue.length){
                this.createPatientView(this._patientQueue.shift());
            };
        }
        private function createPatientView(_arg1:Object):void{
            this._isNextPatientAllowed = false;
            dispatch(new InfoEvent(EventConstants.SELECT_PET_DATA, null, _arg1));
        }
        protected function track(_arg1:String=null):void{
            var _local2:Object = new Object();
            _local2.type = "computer";
            _local2.subtype1 = _arg1;
            dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.EVENT, _local2));
        }

    }
}//package net.wooga.pets.views 
